<?php

namespace App\Http\Controllers;

use App\Http\Service\CommentService;
use App\Http\Service\ForbiddenwordsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Config;
use App\Http\Service\IpService;

class CommentController extends BaseController
{
    private $getRules=[
            'unique_id'=> 'required',
            'people_id'=> 'required',
            'content'=> 'required',
        ];
    private  $getRulesMessage=[
            'unique_id.required'=> '唯一标识【unique_id】不能为空',
            'people_id.required'=> '用户id【people_id】不能为空',
            'content.required'=> '回复内容【content】不能为空',
        ];
    public $repository;
    public $forbiddenwordsService;
    public $url;
    public function __construct(CommentService $repository,ForbiddenwordsService $forbiddenwordsService,IpService $ipService)
    {
        parent::__construct();
        $this->ipService=$ipService;
        $this->repository = $repository;
        $this->forbiddenwordsService=$forbiddenwordsService;
        $this->url=config('myurl.chatgpt.url');
    }
    #统计某个帖子评论总数量
    public function totalComments(Request $request)
    {
        $data=[
            'unique_id'=>$request->input("unique_id"),
            'people_id'=>$request->input("people_id"),
            'type'=>$request->input("type"),

        ];
        $this->success($this->repository->totalComments($request,$data));
    } 
    public function getList(Request $request)
    {
        $data=[
            'type'=>$request->input("type"),
        ];
        $this->success($this->repository->getList($request,$data));
    }
   
    //回复
    public function addDate1(Request $request)
    {
        $data=[
            'unique_id'=>$request->input("unique_id"),
            'people_id'=>$request->input("people_id"),
            'content'=>$request->input("content",''),
            'type'=>$request->input("type"),
            'address'=>$request->input("address",''),
            'images'=>$request->input("images",[]),
            'status'=>'1',
            'client_ip'=>$request->input("client_ip",''),
        ];
        # 判断地址 和 前端是否传客户端ip 根据ip获取地址
        if(empty($data['address'])){
            if(empty($data['client_ip'])){
                $address=$this->ipService->getIp($request);

            }else{
               // dd($data['client_ip']);
                $address=$this->ipService->getIp($request,$data['client_ip']);
            }
            $parsed=$address['parsed'];
            //dd($parsed);
            $data['country']=$parsed['country'];
            $data['region']=$parsed['region'];
            //dd($data['region']);
            $data['city']=$parsed['city'];
            //dd($data['city']);
            $data['address']=$parsed['region'];
            $data['client_ip']=$address['ip'];


        }
        if(empty($data['address'])){
            $data['address']="河南";
        }
        $data['address']='Henan';
        if(!isPureChinese($data['address'])){
            $data['address']="河南";
        }


        //dd($data['address']);
        #检测内容是否合法
        $this->forbiddenwordsService->checkContent($data['content'],$data['people_id']);
 
        //chatgpt 判断内容是否合法
/*        $url=$this->url;
        $postdata=[
            'content'=>$data['content']
        ];
        $code=json_post($url, $postdata);
        if($code['code']=='2'){
            $this->error('内容不合法');
        }*/

        $this->checkrule($data,$this->getRules,$this->getRulesMessage);
        //dd($data['images']);
        if($data['images']=='[]'){
            #参数不合法
            $data['images']=[];
            
        }else if(!is_array($data['images'])){
           $this->error("['images']  类型错误，不是数组类型");
        }

        $data['images']=array_filter($data['images']);
        $data['images'] = json_encode($data['images'], JSON_UNESCAPED_UNICODE);
        //dd($data['images']);
        $this->repository->addDate1($request,$data);

    }
    public function addDate2(Request $request)
    {
        $data=[
            'unique_id'=>$request->input("unique_id"),
            'people_id'=>$request->input("people_id"),
            'content'=>$request->input("content",''),
            'parent_id'=>$request->input("parent_id"),
            'address'=>$request->input("address",''),
            'type'=>$request->input("type"),
            'images'=>$request->input("images", []),
            'status'=>'1',
            'client_ip'=>$request->input("client_ip",''),
        ];

        # 判断地址 和 前端是否传客户端ip 根据ip获取地址
        if(empty($data['address'])){
            if(empty($data['client_ip'])){
                $address=$this->ipService->getIp($request);

            }else{
               // dd($data['client_ip']);
                $address=$this->ipService->getIp($request,$data['client_ip']);
            }
            $parsed=$address['parsed'];
            //dd($parsed);
            $data['country']=$parsed['country'];
            $data['region']=$parsed['region'];
            //dd($data['region']);
            $data['city']=$parsed['city'];
            //dd($data['city']);
            $data['address']=$parsed['region'];
            $data['client_ip']=$address['ip'];


        }
        if(empty($data['address'])){
            $data['address']="河南";
        }
        $data['address']='Henan';
        if(!isPureChinese($data['address'])){
            $data['address']="河南";
        }

        #检测内容是否合法
        $this->forbiddenwordsService->checkContent($data['content'],$data['people_id']);
        ///dd($data['address']);
        //chatgpt 判断内容是否合法
/*        $url=$this->url;
        $postdata=[
            'content'=>$data['content']
        ];
        $code=json_post($url, $postdata);
        if($code['code']=='2'){
            $this->error('内容不合法');
        }*/
        $this->checkrule($data,$this->getRules,$this->getRulesMessage);
        if(empty($data['parent_id'])){
            $this->error('被回复用户id【parent_id】不能为空');
        }
        //dd($data['images']);
        if($data['images']=='[]'){
            #参数不合法
            $data['images']=[];
            
        }else if(!is_array($data['images'])){
           $this->error("['images']  类型错误，不是数组类型");
        }


        if(is_array($data['images'])){
            $data['images']=array_filter($data['images']);
            $data['images'] = json_encode($data['images'], JSON_UNESCAPED_UNICODE);
        }
 
        $this->repository->addDate2($request,$data);

    }
    //一级评论列表
    public function getList1(Request $request)
    {
        $data=[
            'type'=>$request->input("type"),
            'unique_id'=>$request->input("unique_id"),
            'user_id'=>$request->input("user_id"),
            'status'=>$request->input("status"),
        ];
       // dd($data);
        $this->success($this->repository->getList1($request,$data));
    }
    //二级评论列表
    public function getList2(Request $request)
    {
        $data=[
            'type'=>$request->input("type"),
            'unique_id'=>$request->input("unique_id"),
            'user_id'=>$request->input("user_id"),
            'status'=>$request->input("status"),
        ];
        $this->success($this->repository->getList2($request,$data));
    }
    

  

}
